// pages2/pages/mall/combine/index.js
// pages/mall/shopDetail/index.js
const $api = require('../../../../request/api').API;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    combineInfo: {
      combineId: '',
      combineCode: '',
      combineName: '',
      combineDesc: '',
      combineType: '',
      remark: '',
      sort: '',
      saleState: '',
      mainImg: '',
      name: '',
      salePrice: 0,
      goodsList: []
    },
    goodsList: [],
    nums: 1,
    totalPrice: '',
    id: '',
    combinePrice: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id
      });
      this.getCombineInfo();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},
  // 通过商品id来获取对应的数组索引，不存在就返回-1
  getIndexById(arr, id, prop = 'id') {
    let tempIndex = -1;
    arr.find((item, index) => {
      if (item[prop] == id) {
        tempIndex = index;
        return true;
      }
    });
    return tempIndex;
  },
  // 获取商品信息
  getCombineInfo() {
    let params = {
      id: this.data.id
    };
    $api.getCombineInfo(params).then(res => {
      this.setData({
        combineInfo: res.data.data,
        goodsList: res.data.data.goodsList,
        nums: 1
      });
      let totalPrice = 0;
      this.data.goodsList.forEach(item => {
        item.nums = 1;
        totalPrice = totalPrice + item.salePrice;
      });
      this.setData({
        totalPrice: totalPrice,
        combinePrice: totalPrice
      });
    });
  },
  // 新增
  addNum(e) {
    this.data.nums += 1;
    let val = this.data.combinePrice * this.data.nums;
    this.setData({
      nums: this.data.nums,
      totalPrice: parseFloat(val.toFixed(2))
    });
  },
  // 减少
  subNum(e) {
    if (this.data.nums <= 1) return;
    this.data.nums -= 1;
    let val = this.data.combinePrice * this.data.nums;
    this.setData({
      nums: this.data.nums,
      totalPrice: parseFloat(val.toFixed(2))
    });
  },
  // 输入数量
  numChange(e) {
    let nums = Number(e.detail.value) || 1;
    let val = this.data.combinePrice * nums;
    this.setData({
      nums,
      totalPrice: parseFloat(val.toFixed(2))
    });
  },
  // 直接下单去订单结算页面
  toSubmitOrderPage() {
    // let combine = encodeURIComponent(JSON.stringify(this.data.goodsList))
    let userRole = wx.getStorageSync('user_role');
    if (userRole == 2) {
      wx.navigateTo({
        url: '/subcontract/home/pages/submitOrder/index?combineId=' + this.data.id + '&nums=' + this.data.nums
      });
    } else if (userRole == 3) {
      wx.navigateTo({
        url: '/pages2/pages/mall/businessSubmitOrder/index?combineId=' + this.data.id + '&nums=' + this.data.nums
      });
    }
  },
  // 图片预览
  previewImage() {
    wx.previewImage({
      current: this.data.combineInfo.mainImg, // 当前显示图片的 http 链接
      urls: [this.data.combineInfo.mainImg] // 需要预览的图片 http 链接列表
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});
